﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars;
using System.IO;
using System.Windows.Forms;

namespace System
{
    public static class BarExt
    {
        public static string GetLayoutXml(this BarManager view)
        {
            string layout = "";
            try
            {
                MemoryStream stream = new MemoryStream();
                view.SaveLayoutToStream(stream);
                stream.Seek(0, SeekOrigin.Begin);
                using (StreamReader rdr = new StreamReader(stream))
                {
                    layout = rdr.ReadToEnd();
                }
                stream.Close();
            }
            catch { }
            return layout;
        }
        public static void SetLayoutXml(this BarManager view, string xml)
        {
            try
            {
                MemoryStream stream = new MemoryStream();
                using (StreamWriter str = new StreamWriter(stream))
                {
                    str.Write(xml);
                    str.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    view.RestoreLayoutFromStream(stream);
                    stream.Close();
                }
            }
            catch { }
        }

        public static void SetItemPosition(this Bar bar, BarItem item, int position)
        {
            LinkPersistInfo info =  null;
            foreach (LinkPersistInfo i in bar.LinksPersistInfo)
            {
                if (i.Item == item)
                {
                    info = i;
                    break;
                }
            }
            if (info != null)
            {
                if (bar.Manager != null)
                    bar.Manager.BeginInit();
                bar.LinksPersistInfo.Remove(info);
                
                if (position > bar.LinksPersistInfo.Count)
                    bar.LinksPersistInfo.Add(info);
                else
                    bar.LinksPersistInfo.Insert(position, info);
                if (bar.Manager != null)
                    bar.Manager.EndInit();
            }
        }

        public static void MergeBars(this BarManager parent, BarManager child, params string[] bars)
        {
            if (parent != null && child != null && bars != null)
            {
                Bar bp;
                Bar bc;
                foreach (string bar in bars)
                {
                    bp = parent.Bars[bar];
                    bc = child.Bars[bar];
                    if (bc != null && bp != null)
                    {
                        bp.Merge(bc);
                        bc.Visible = (bc.HideWhenMerging != DevExpress.Utils.DefaultBoolean.False ? false : bc.VisibleLinks.Count > 0);
                        bp.Visible = bc.VisibleLinks.Count > 0;
                    }
                }
            }
        }

        public static void UnMergeBars(this BarManager parent, BarManager child, params string[] bars)
        {
            if (parent != null && child != null && bars != null)
            {
                Bar bp;
                Bar bc;
                foreach (string bar in bars)
                {
                    bp = parent.Bars[bar];
                    bc = child.Bars[bar];
                    if (bc != null && bp != null)
                    {
                        bp.UnMerge();
                        bc.Visible = true;
                        bp.Visible = bp.VisibleLinks.Count > 0;
                    }
                }
            }
        }

        /// <summary>
        /// Merge bar with bar of Active MDI child
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="bars"></param>
        public static void MergeBars(this BarManager parent, params string[] bars)
        {
            if (parent != null)
                parent.MergeBars(parent.ActiveMdiChild.FindBarManager(), bars);
        }
        /// <summary>
        /// UnMerge bar with bar of Active MDI child
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="bars"></param>
        public static void UnMergeBars(this BarManager parent, params string[] bars)
        {
            if (parent != null)
                parent.UnMergeBars(parent.ActiveMdiChild.FindBarManager(), bars);
        }
        
        public static BarManager FindBarManager(this Form form)
        {
            if (form != null)
            {
                foreach (Control c in form.Controls)
                {
                    if (c is BarDockControl)
                        return ((BarDockControl)c).Manager;
                }
            }
            return null;
        }
    }
}
